package com.sanq.product.cab.tools.prop;

import java.io.*;
import java.util.Properties;

/**
 * version: Properties操作
 * ---------------------
 *
 * @author sanq.Yan
 * @date 2020/1/26
 */
public class PropUtil {

    private static Properties properties;


    synchronized static private void loadProp(String path) {
        properties = new Properties();
        InputStream in = null;
        try {
            in = new FileInputStream(path);
            properties.load(in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getProperty(String path, String key) {
        if (null == properties) {
            loadProp(path);
        }
        return properties.getProperty(key);
    }

    public static void saveOrUpdate(String path, String key, String value) {
        if (null == properties) {
            loadProp(path);
        }

        properties.setProperty(key, value);
        FileOutputStream oFile = null;
        try {
            oFile = new FileOutputStream(path);
            properties.store(oFile, "");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
